package com.atwork.controller;

import com.atwork.model.vo.ShiroUser;
import com.atwork.service.ShiroUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @CreateBy: zhaojun
 * @CreateTime: 2018-08-04
 * @Desc: BaseController
 */

public class BaseController {
    @Autowired
    private ShiroUserService shiroUserService;

    protected ShiroUser getShiroUser(String loginName){
        ShiroUser shiroUser = shiroUserService.getShiroUser(loginName);
        return shiroUser;
    }
    protected ShiroUser getShiroUser(){
        String username = getHttpServletRequest().getSession().getAttribute("loginName").toString();
        ShiroUser shiroUser = shiroUserService.getShiroUser(username);
        return shiroUser;
    }

    protected HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    protected HttpServletResponse getHttpServletResponse() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }

    protected HttpSession getSession() {
        return getHttpServletRequest().getSession();
    }

    protected HttpSession getSession(Boolean flag) {
        return getHttpServletRequest().getSession(flag);
    }

    protected String getPara(String name) {
        return getHttpServletRequest().getParameter(name);
    }
}
